<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 * 小程序端，小程序端不会自动发送cookie，需要手动发送sessionID，此处访问首页，成功则返回sessionID，失败则返回0
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'login';
if($_RQ['aligid'] || $_RQ['wxgid'] || $_RQ['wxapp_id']){
	$wxapp_id=intval($_RQ['aligid'])?:intval($_RQ['wxgid']);
	$wxapp_id=$wxapp_id?:intval($_RQ['wxapp_id']);
	$_SESSION['aliapp_id']=$_SESSION['appgid']=$wxapp_id;
	if(!$_SESSION['aliapp_id']){
		exit('error_baiduapp:'.$_RQ['aligid']);
	}
}else{
	return false;
}
//H5小程序传openid
if($_RQ['app_openid'] && !$_SESSION['openid']){
	$_SESSION['openid']=$_RQ['app_openid'];
	$fans=pdo_get('core_fans',array('openid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid'],'aligid'=>$_SESSION['appgid']),array('id','mid'));
	if($fans['mid']){
		$mid= $fans['mid'];
	}
	if($mid){
		model('core/member')->login($mid);
	}
	//$url=model('core/member')->ali_auth_info_url();
	if($url){
		//header('location:'.$url);exit;
	}
	return false;
}
$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['aliapp_id'],'uniacid'=>$_SESSION['uniacid']));
if(!$wxapp && !$_SESSION['aliappopenid']){
	return false;
	exi('指定小程序不存在！'.$_SESSION['aliapp_id']);
}
if($_RQ['op']=='login'){
	if($_RQ['code'] && !$_SESSION['openid']){
		//如果有code，根据code获取粉丝openid
		if(!$wxapp['ali_appid'] || !$wxapp['alipay_private_key'] || !$wxapp['alipay_public_key']){
			exi('支付宝小程序的appid，私钥，公钥都必须填写','error');
		}
		$setting=array(
			'alipay_appid'=>$wxapp['ali_appid'],
			'alipay_private_key'=>$wxapp['alipay_private_key'],
			'alipay_public_key'=>$wxapp['alipay_public_key'],
		);
		$result=model('jy_alipay/alipay',$setting)->aliapp_oauth($_RQ['code']);
		if(empty($result['user_id'])){
			exi('获取粉丝openid失败：','error');
		}
		if($result['user_id']){
			$_SESSION['openid']=$result['user_id'];
			$_SESSION['aliappopenid']=$result['user_id'];
			$_SESSION['session_key']=$result['session_key'];
			$mid=false;
			if($result['unionid']){
				$_SESSION['unionid']=$result['unionid'];
				$mid=pdo_getcolumn('core_members',array('unionid'=>$result['unionid'],'uniacid'=>$_SESSION['uniacid']),'id');
			}
			$fans=pdo_get('core_fans',array('openid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid'],'aligid'=>$_SESSION['aliapp_id']),array('id','mid'));
			if($fans){
				if(!$mid && $fans['mid']){
					$mid= $fans['mid'];
				}
			}else{
				$fansdata=array(
					'uniacid'=>$_SESSION['uniacid'],
					'aligid'=>$_SESSION['aliapp_id'],
					'openid'=>$_SESSION['openid'],
					'type'=>6,
					'createtime' => TIMESTAMP,
				);
				if($mid){
					$fansdata['mid']=$mid;
				}
				if($result['unionid']){
					$fansdata['unionid']=$result['unionid'];
				}
				pdo_insert('core_fans',$fansdata);
			}
			if($mid){
				model('core/member')->login($mid);
			}
		}
		$sid=session_id();
		if(!$_SESSION['base_key_str']){
			$_SESSION['base_key_str']=str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_');
		}
		$return=array('ssid'=>$sid,'session_key'=>$_SESSION['session_key'],'openid'=>$_SESSION['openid'],'uid'=>$_SESSION['uid'],'token'=>$_SESSION['token'],'base_key_str'=>$_SESSION['base_key_str']);
		if($wxapp['plugin_name'] && $wxapp['plugin_name'] !='core'){
			$_SESSION['current_plugin']=$wxapp['plugin_name'];
			$file=ROOT_PLUGIN.$wxapp['plugin_name'].'/model/custom_mobile_menu.php';
			if(is_file($file)){
				$return['menus']=plugin($wxapp['plugin_name'])->custom_mobile_menu();
			}
		}
		exi($return);
	}
}